package fun.shuofeng.juc.completableFuture;

import java.util.concurrent.*;

/**
 * @author 朔风
 * @date 2024-02-28 23:25
 */
public class CompletableFutureBuildDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);


        // CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        //     System.out.println("当前线程 " + Thread.currentThread().getName());
        //     try {
        //         TimeUnit.SECONDS.sleep(3);
        //     } catch (InterruptedException e) {
        //         throw new RuntimeException(e);
        //     }
        // },threadPool);
        // System.out.println(completableFuture.get());

        CompletableFuture<String> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程 " + Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "hello supplyAsync";
        },threadPool);
        System.out.println(uCompletableFuture.get());

        threadPool.shutdown();
        System.out.println("主线程结束");
    }
}
